﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : MonoBehaviour, IInput
{
    /// <summary>
    /// 移动输入
    /// </summary>
    public Action<Vector2> OnMovementInput { get; set; }
    /// <summary>
    /// 方向输入
    /// </summary>
    public Action<Vector3> OnMoveMentDirectionInput { get; set; }

    private void Start()
    {
        //固定光标到屏幕中心
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update()
    {
        GetMovementInput();
        GetMovementDirection();
    }

    /// <summary>
    /// 运动方向输入
    /// </summary>
    private void GetMovementDirection()
    {
        //摄像机射线
        var cameraForwardDirection = Camera.main.transform.forward;
        //绘制方向射线
        Debug.DrawRay(Camera.main.transform.position, cameraForwardDirection * 10, Color.red);
        /**计算移动方向
         * 摄像机方向 向量  （相乘）  XZ轴平面向量
         * 得到 平行于XY平面的方向向量
        **/
        var directionToMovement = Vector3.Scale(cameraForwardDirection, (Vector3.right + Vector3.forward));
        Debug.DrawRay(Camera.main.transform.position, directionToMovement * 10, Color.blue);
        OnMoveMentDirectionInput?.Invoke(directionToMovement);
    }
    /// <summary>
    /// 运动输入
    /// </summary>
    private void GetMovementInput()
    {
        //纵横输入
        Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        //执行委托，计算运动量输入
        OnMovementInput?.Invoke(input);
    }
}
